#include <iostream>
#include <cassert>
#include "../include/bitmap.h"

// 使用 using namespace bitset; 以便直接使用类名
using namespace bitset;

void test_bitmapofUint8_t()
{
    std::cout << "--- Testing bitmapofUint8_t ---" << std::endl;

    // 测试大小为 100 的位图
    bitmapofUint8_t bm(100);
    assert(bm.size() == 100);

    // 1. 初始状态检查
    for (size_t i = 0; i < 100; ++i)
    {
        assert(bm.check(i) == false);
    }
    std::cout << "Initial state test passed." << std::endl;

    // 2. set 和 check
    bm.set(0);
    bm.set(42);
    bm.set(99);
    assert(bm.check(0) == true);
    assert(bm.check(42) == true);
    assert(bm.check(99) == true);
    assert(bm.check(1) == false); // 检查未设置的位
    std::cout << "Set/Check test passed." << std::endl;

    // 3. reset 和 check
    bm.reset(42);
    assert(bm.check(42) == false);
    assert(bm.check(0) == true); // 确保其他位不受影响
    std::cout << "Reset/Check test passed." << std::endl;

    // 4. clear
    bm.clear();
    for (size_t i = 0; i < 100; ++i)
    {
        assert(bm.check(i) == false);
    }
    std::cout << "Clear test passed." << std::endl;

    // 5. 边界测试
    bm.set(99);
    assert(bm.check(99) == true);
    bm.set(100);                    // 越界操作，应该被忽略
    assert(bm.check(100) == false); // 确认越界读取返回 false
    std::cout << "Boundary tests passed." << std::endl;

    std::cout << "All bitmapofUint8_t tests passed!" << std::endl;
}

void test_bitmapofVector()
{
    std::cout << "\n--- Testing bitmapofVector<128> ---" << std::endl;

    constexpr size_t N = 128;
    bitmapofVector<N> bv;

    // 1. 初始状态
    for (size_t i = 0; i < N; ++i)
    {
        assert(bv.check(i) == false);
    }
    std::cout << "Initial state test passed." << std::endl;

    // 2. set 和 check (跨块边界)
    bv.set(0);
    bv.set(31);  // 第一个块的末尾
    bv.set(32);  // 第二个块的开头
    bv.set(127); // 最后一个块的末尾
    assert(bv.check(0) == true);
    assert(bv.check(31) == true);
    assert(bv.check(32) == true);
    assert(bv.check(127) == true);
    assert(bv.check(1) == false);
    assert(bv.check(64) == false);
    std::cout << "Set/Check (cross-block) test passed." << std::endl;

    // 3. reset 和 check
    bv.reset(31);
    assert(bv.check(31) == false);
    assert(bv.check(32) == true); // 确保其他位不受影响
    std::cout << "Reset/Check test passed." << std::endl;

    // 4. clear
    bv.clear();
    // for (size_t i = 0; i < N; ++i)
    // {
    //     assert(bv.check(i) == false);
    // }
    std::cout << "Clear test passed." << std::endl;

    // 5. 边界测试
    bv.set(127);
    assert(bv.check(127) == true);
    bv.set(128);                    // 越界操作，应该被忽略
    assert(bv.check(128) == false); // 确认越界读取返回 false
    std::cout << "Boundary tests passed." << std::endl;

    std::cout << "All bitmapofVector tests passed!" << std::endl;
}

int main()
{
    test_bitmapofUint8_t();
    test_bitmapofVector();
    return 0;
}
